import 'package:flutter/material.dart';

import '../common/flatPanel.dart';
import '../entry.dart';
import '../models/changeLogItem.dart';
import '../utils/screw.dart';

class ChangeLogEdit extends StatefulWidget {
  @override
  _ChangeLogEditState createState() {
    return _ChangeLogEditState();
  }
}

class _ChangeLogEditState extends State<ChangeLogEdit> {
  final entry = Entry.ChangeLog;
  final baseUrl = Entry.BaseUrl + Entry.ChangeLog;
  final _entry = TextEditingController();
  final _date = TextEditingController();
  final _version = TextEditingController();
  final _detail = TextEditingController();
  final GlobalKey _formKey = new GlobalKey<FormState>();
  var _entries = <String>[];

  @override
  initState() {
    super.initState();
    _date.text = Screw.genDateString();
    _getEntries();
  }

  // https://www.cnblogs.com/mengqd/p/12506315.html
  _openDatePicker() async {
    var result = await showDatePicker(
        context: context,
        initialDate: DateTime.now(),
        firstDate: DateTime(2020),
        lastDate: DateTime(2021));
    _date.text = Screw.getDateString(result);
  }

  _getEntries() async {
    var data = await Screw.getList(baseUrl + '/entries', entry, 'topics');
    setState(() {
      _entries = data.cast<String>();
    });
  }

  _onSubmit() {
    var item = ChangeLogItem(
      entry: _entry.text,
      date: _date.text,
      version: _version.text,
      detail: _detail.text,
    );

    Screw.postData(baseUrl, item.toMap());
    (_formKey.currentState as FormState).reset();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(Parts.ChangeLogEdit),
      ),
      body: FlatPanel(
        child: Form(
          key: _formKey,
          child: Column(
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: TextFormField(
                      controller: _entry,
                      decoration: InputDecoration(
                          labelText: '组件名',
                          hintText: '组件名',
                          prefixIcon: Icon(Icons.title)),
                      validator: (v) => v.trim().length > 0 ? null : '组件名不能为空！',
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: DropdownButton(
                        items: _entries
                            .map((e) =>
                                DropdownMenuItem(child: Text(e), value: e))
                            .toList(),
                        onChanged: (v) {
                          setState(() {
                            _entry.text = v;
                          });
                        }),
                  ),
                ],
              ),
              TextFormField(
                controller: _version,
                decoration: InputDecoration(
                    labelText: '版本号',
                    hintText: '版本号',
                    prefixIcon: Icon(Icons.bookmark)),
                validator: (v) => v.trim().length > 0 ? null : '版本号不能为空！',
              ),
              TextFormField(
                controller: _detail,
                decoration: InputDecoration(
                    labelText: '版本说明',
                    hintText: '版本说明',
                    prefixIcon: Icon(Icons.details)),
                validator: (v) => v.trim().length > 0 ? null : '说明不能为空！',
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: TextFormField(
                      controller: _date,
                      decoration: InputDecoration(
                          labelText: '日期',
                          hintText: '更新日期',
                          prefixIcon: Icon(Icons.date_range)),
                      keyboardType: TextInputType.datetime,
                      validator: (v) => v.trim().length > 0 ? null : '日期不能为空！',
                    ),
                  ),
                  FlatButton.icon(
                      label: Text('Date'),
                      icon: Icon(Icons.calendar_today),
                      onPressed: _openDatePicker)
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: FlatButton(
                        child: Text('清空', style: TextStyle(fontSize: 24)),
                        onPressed: () {
                          (_formKey.currentState as FormState).reset();
                          _entry.text = '';
                        }),
                  ),
                  Expanded(
                    flex: 1,
                    child: FlatButton(
                      child: Text('提交',
                          style: TextStyle(fontSize: 24, color: Colors.blue)),
                      onPressed: () {
                        if ((_formKey.currentState as FormState).validate())
                          _onSubmit();
                      },
                    ),
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}
